package com.whsr.readservice.dao;

import com.whsr.readservice.service.PageConfigService;
import com.whsr.readservice.vo.PageConfigGroup;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface PageConfigGroupMapper {

    @Insert("insert into page_config_group (id, name, parent_id, group_icon, data_flag, create_time, update_time, position, templet, level, to_url) values (#{id}, #{name}, #{parentId}, #{groupIcon}, #{dataFlag}, #{createTime}, #{updateTime}, #{position}, #{templet}, #{level}, #{toUrl})")
    @SelectKey(statement="SELECT LAST_INSERT_ID()",keyProperty="id",before=false,resultType=Integer.class)
    public int add(PageConfigGroup configGroup);

    @Update("update page_config_group set name=#{name}, parent_id=#{parentId}, group_icon=#{groupIcon}, data_flag=#{dataFlag}, create_time=#{createTime}, update_time=#{updateTime}, position=#{position}, templet=#{templet}, level=#{level}, to_url=#{toUrl} where id=#{id}")
    public int update(PageConfigGroup configGroup);

    @Delete("delete from page_config_group where id=#{id}")
    int remove(int id);

    @Delete("delete from page_config_group where parent_id=#{id}")
    int removeChildren(int id);

    @Select({"<script>",
            "SELECT * FROM page_config_group ",
            "WHERE 1=1",
            "<when test='title!=null'>",
            "AND name = #{name}",
            "</when>",
            "</script>"})
    @Results({
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "groupIcon", column = "group_icon"),
            @Result(property = "dataFlag", column = "data_flag"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "toUrl", column = "to_url")
    })
    List<PageConfigGroup> queryConfigGroup(Object name);

    @Select("select * from page_config_group where id=#{id}")
    @Results({
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "groupIcon", column = "group_icon"),
            @Result(property = "dataFlag", column = "data_flag"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "toUrl", column = "to_url")
    })
    PageConfigGroup getConfigGroupById(int id);

    @Select("SELECT * FROM page_config_group WHERE parent_id = #{parentId}")
    @Results({
            @Result(property = "parentId", column = "parent_id"),
            @Result(property = "groupIcon", column = "group_icon"),
            @Result(property = "dataFlag", column = "data_flag"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<PageConfigGroup> getConfigGroupByParentId(int parentId);


    @Select({"<script>",
            "select distinct a.* from ( ",
            "SELECT * FROM article ",
            "WHERE 1=1",
            "<when test='categoryId!=-1 and categoryId!=1 and categoryId!=2'>",
            " AND category_id = #{categoryId} ",
            "</when>",
            "<when test='categoryId==1 and categoryId==2'>",
            " AND EXISTS (SELECT 1 from category c where c.id = category_id and c.p_id= #{categoryId})",
            "</when>",
            "<when test='key!=null'>",
            " AND (book_name like CONCAT('%',#{key},'%') or book_author like CONCAT('%',#{key},'%') or book_class like CONCAT('%',#{key},'%')) ",
            " union " ,
            "select a.* from article a, (select f.* from category d, category f where d.id=f.p_id and d.name like CONCAT('%',#{key},'%')) c where a.category_id = c.id ",
            "</when>",
            " ) a order by a.id",
            "<when test='startIndex > -1 &amp;&amp; endIndex > 0'>",
            " limit #{startIndex}, #{endIndex} ",
            "</when>",
            "</script>"})
    List<Map<String,Object>> getArtByPage(@Param("startIndex") int startIndex, @Param("endIndex") int endIndex, @Param("categoryId") int categoryId, @Param("key") String key);

    @Select({"<script>",
            "select count(a.id) from ( ",
            "SELECT * FROM article ",
            "WHERE 1=1",
            "<when test='categoryId!=-1 and categoryId!=1 and categoryId!=2'>",
            " AND category_id = #{categoryId} ",
            "</when>",
            "<when test='categoryId==1 and categoryId==2'>",
            " AND EXISTS (SELECT 1 from category c where c.id = category_id and c.p_id= #{categoryId})",
            "</when>",
            "<when test='key!=null'>",
            " AND (book_name like CONCAT('%',#{key},'%') or book_author like CONCAT('%',#{key},'%') or book_class like CONCAT('%',#{key},'%')) ",
            " union " ,
            "select a.* from article a, (select f.* from category d, category f where d.id=f.p_id and d.name like CONCAT('%',#{key},'%')) c where a.category_id = c.id ",
            "</when>",
            " ) a ",
            "</script>"})
    Object getCountArtByPage(@Param("categoryId") int categoryId, @Param("key") String key);
}
